package com.suoluo.zenadmin.service.task;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;

import com.suoluo.zenadmin.dao.RequestLogDAO;
import com.suoluo.zenadmin.oss.OSSService;

/**
 * 删除数据库中的请求日志，和OSS服务器上的图片日志
 * @author Administrator
 *
 */
public class DeleteLogTask {

	@Resource(name="ossService")
	private OSSService ossService;
	
	@Autowired
	private RequestLogDAO requestLogDAO;
	
	
	private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
	
	/**
	 * 删除请求日志，和OSS图片的访问日志
	 * 1.请求日志保留三天的
	 */
	public void run(){
//		System.out.println("curTimes:"+System.currentTimeMillis());
		Calendar cal = getCurrentTime();
		cal.add(Calendar.DAY_OF_MONTH, -3);
//		System.out.println(format.format(cal.getTime()));
		//删除系统请求日志
		requestLogDAO.deleteRequestLog(cal.getTime());
		
		//删除OSS的图片日志
//		zentravel.cnzentravel-img2014-04-08-11-00-00-0001
		for (int i = 0;i<24;i++){
			String key = "zentravel.cnzentravel-img" + format.format(cal.getTime())+"-0001";
//			System.out.println(key);
			ossService.deleteImage(key);
			cal.add(Calendar.HOUR_OF_DAY, 1);
		}
	}
	
//	private String buildOSSLogKey(Calendar cal){
//		
//	}
	
	private Calendar getCurrentTime(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal;
	}
}
